<template>
  <div ref="wrapper">
    <div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: "scroller",
  data() {
    return {
      scroller: {
        type: Object,
        default() {
          return {}
        }
      },
      message: "hahahah",
      pullUpLoad: {
          type: Boolean,
          default: false
      }
    };
  },
  //接收父组件来的值
  props: {
    probeType: {
      type: Number,
      default: 0
    }
  },
  mounted() {
    //1.创建BScroll对象
    this.scroller = new BScroll(this.$refs.wrapper, {
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
      click: true //解决better-scroll不能点击的问题
    });

    //2.监听滚动的位置
    if (this.probeType === 2 || this.probeType === 3){
      this.scroller.on("scroll", position => {
        //console.log(postion);
        this.$emit("scroll", position);
       });
    }

    //3.监听scroll滚动到底部
    if (this.pullUpLoad) {
      this.scroller.on('pullingUp', () => {
        // console.log('上拉加载更多');
        this.$emit('pullUpLoad')
      })
    }


  },
  methods: {
    scrollTo(x, y, time) {
        this.scroller.scrollTo(x, y, time)
    },
    finishPullUp() {
        this.scroller.finishPullUp()
    },
    refresh() {
      // console.log('-------');
      this.scroller.refresh()
    },
    getScrollY() {
      return this.scroller.y
    }
  }
};
</script>

<style>
</style>
